package com.wh.simpleredis.manager;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/10 18:54
 * @Description
 */
@Component
public class RedissonLock {
    private Logger logger= LoggerFactory.getLogger(RedissonLock.class);
    @Autowired
    private RedissonClient redissonClient;

    public boolean lock(String lockname){
       try{
           RLock lock = redissonClient.getLock(lockname);
           lock.lock(30, TimeUnit.SECONDS);
       }catch (Exception e){
           logger.info(e.getMessage());
           return false;
       }
       return true;
    }
    public boolean unlock(String lockname){
        try{
            RLock lock = redissonClient.getLock(lockname);
            lock.unlock();
        }catch (Exception e){
            logger.info(e.getMessage());
            return false;
        }
        return true;
    }
}
